//
//  YXTurnoverTableView.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/25.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXTurnoverTableView.h"
#import "YXTradingModel.h"
@interface YXTurnoverTableView ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation YXTurnoverTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self = [super initWithFrame:frame style:style]) {
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.dataSource = self;
        self.delegate = self;
        [self registerClass:[YXTurnoverTableViewCell class] forCellReuseIdentifier:@"YXTurnoverTableViewCell"];
    }
    return self;
}

- (void)setTurnoverArr:(NSMutableArray *)turnoverArr {
    _turnoverArr = turnoverArr;
    _turnoverArr=(NSMutableArray *)[[_turnoverArr reverseObjectEnumerator] allObjects];
    [self scrollRectToVisible:(CGRectMake(0, 0, 1, 1)) animated:NO];
    [self reloadData];
}

#pragma mark - UITableView data source -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.turnoverArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YXTurnoverTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YXTurnoverTableViewCell" forIndexPath:indexPath];
    YXTradingModel *model = self.turnoverArr[indexPath.row];
    if ([model.transaction_amounts isEqualToString:@"0"]) {
        [self.turnoverArr removeObject:model];
        [self reloadData];
    }else {
        cell.timeLab.text = model.transactionTimeString;
        cell.turnoverLab.text = [NSString stringWithFormat:@"%@元",model.transaction_amounts];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 30;
}

@end




@implementation YXTurnoverTableViewCell

- (UILabel *)timeLab {
    if (!_timeLab) {
        _timeLab = [UILabel setLabelWithText:@"" fontSize:14 color:color_textThree];
        [self.contentView addSubview:_timeLab];
        [_timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView.mas_centerY);
            make.left.equalTo(self.contentView.mas_left).offset(50);
            [_timeLab sizeToFit];
        }];
    }
    return _timeLab;
}

- (UILabel *)turnoverLab {
    if (!_turnoverLab) {
        _turnoverLab = [UILabel setLabelWithText:@"" fontSize:14 color:color_textThree];
        [self.contentView addSubview:_turnoverLab];
        [_turnoverLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView.mas_centerY);
            make.right.equalTo(self.contentView.mas_right).offset(-20);
            [_turnoverLab sizeToFit];
        }];
    }
    return _turnoverLab;
}

@end


